/*
 * 本example主要是用来展示如何使用DGGSFeature库实现对矢量数据的存储管理和保存
 * 本example依赖于kernel库
 */

#include"DGGSFeatureClass.hpp"
#include"DGGSFeatureLayer.hpp"
#include"DGGSFeature.hpp"
#include"DGGSGeometry.hpp"
#include"DGGSPoint.hpp"
#include"DGGSPolyLine.hpp"
#include"DGGSPolygon.hpp"

#include"MathDefine.hpp"

// 本样例使用的类及其方法均定义在NNU::DGGS::Feature命令空间内
// 基础的数据结构定义在NNU::DGGS::Base命名空间内
// DGGSGeometryType几何类型在NNU::DGGS命名空间
using namespace NNU::DGGS::Feature;
using namespace NNU::DGGS::Base;
using namespace NNU::DGGS;

/// <summary>
/// 删除一个要素对象
/// </summary>
/// <param name="featureLayer">要素图层</param>
/// <param name="fId">要素id</param>
bool deleteFeature(DGGSFeatureLayer *&featureLayer,int fId)
{
    auto feature = featureLayer->getFeature(fId);
    return featureLayer->eraseFeature(feature);
}

/// <summary>
/// 添加一个要素对象
/// </summary>
/// <param name="featureLayer">要素图层</param>
/// <param name="fId">要素id</param>
/// <param name="codes">格网数据</param>
/// <param name="codeCount">格网数量</param>
bool addFeature(DGGSFeatureLayer *&featureLayer,int fId,DGGSElementCode*codes,int codeCount)
{
    //新建一个feature
    auto featureClass = featureLayer->getFeatureClass();
    auto feature= featureClass->createFeature();
    //设置要素对象的id属性
    feature->setFID(fId);

    //设置几何属性
    switch (featureClass->getGeometryType()) {
        case ST_Point: {
            DGGSPoint point;
            point.setGridCount(codeCount);
            point.setGridData(codes);
            feature->setGeometry(&point);
            break;
        }
        case ST_PolyLine: {
            DGGSPolyLine line;
            line.setGridCount(codeCount);
            line.setGridData(codes);
            feature->setGeometry(&line);
            break;
        }
        case ST_Polygon:{
            DGGSPolygon polygon;
            polygon.setGridCount(codeCount);
            polygon.setGridData(codes);
            feature->setGeometry(&polygon);
            break;
        }
        case ST_Unknown:
            break;
    }
    bool result= featureLayer->addFeature(feature);
    return result;
}

/// <summary>
/// 查找一个要素对象
/// </summary>
/// <param name="featureLayer">要素图层</param>
/// <param name="fId">要素id</param>
DGGSFeature* findFeature(DGGSFeatureLayer *&featureLayer,int fId)
{
    auto feature = featureLayer->getFeature(fId);
    return feature;
}

/// <summary>
/// 更新一个要素对象
/// </summary>
/// <param name="featureLayer">要素图层</param>
/// <param name="fId">要素id</param>
/// <param name="codes">格网数据</param>
/// <param name="codeCount">格网数量</param>
bool updateFeature(DGGSFeatureLayer *&featureLayer,int fId,DGGSElementCode*codes,int codeCount)
{
    //获取feature
    DGGSFeature* feature=findFeature(featureLayer,fId);

    auto geometry=feature->getGeometry();
//    delete geometry;
//    geometry= nullptr;

    //设置几何
    switch (featureLayer->getFeatureClass()->getGeometryType())
    {
        case ST_Point:
        {
            DGGSPoint point;
            point.setGridCount(codeCount);
            point.setGridData(codes);
            feature->setGeometry(&point);
            break;
        }
        case ST_PolyLine:
        {
            DGGSPolyLine line;
            line.setGridCount(codeCount);
            line.setGridData(codes);
            feature->setGeometry(&line);
            break;
        }
        case ST_Polygon:
        {
            DGGSPolygon polygon;
            polygon.setGridCount(codeCount);
            polygon.setGridData(codes);
            feature->setGeometry(&polygon);
            break;
        }
        case ST_Unknown:
            break;
    }

    return true;
}


int main()
{
    DGGSElementType elementType=DGGSElementType::Cell;
    DGGSGridType gridType=DGGSGridType::DGGS_ISEA4H;
    int level=5;
    DGGSGeometryType geometryType = DGGSGeometryType::ST_PolyLine;

    //创建featureClass类
    auto featureClass=new DGGSFeatureClass(level,geometryType,gridType,elementType);

    //创建图层
    auto featureLayer= new DGGSFeatureLayer(*featureClass);;

    //创建要素
    for(int basePartition=1; basePartition < 11; basePartition++)
    {
        int codeCount=0;
        auto codes= new DGGSElementCode[rowMaxSize(level)*fColMaxSize(level,gridType)];
        for (int row=0;row< rowMaxSize(level);row++)
        {
            for(int col=0;col< fColMaxSize(level,gridType);col++)
            {
                DGGSElementCode code;
                code.basePartition=basePartition;
                code.row=row;
                code.col=col;
                code.level=level;
                code.elementType=elementType;
                codes[codeCount]=code;
                codeCount++;
            }
        }

        //添加要素
        addFeature(featureLayer, basePartition, codes, codeCount);

        delete[]codes;
    }

    //更新要素
    int fId=1;
    auto codes= new DGGSElementCode[1];
    codes[0].basePartition=3;
    codes[0].row=1;
    codes[0].col=2;
    codes[0].level=level;
    codes[0].elementType=elementType;
    updateFeature( featureLayer, fId,codes,1);

    //删除要素
    deleteFeature(featureLayer,1);

    return 0;
}